package com.ax.api.route.associator.storeAssociator;


import com.ax.api.route.util.RouteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/auditRecord/")
public class PrescriptionAuditController {
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    /**
     *会员处方记录
     * @param associatorId
     * @return
     */
    @RequestMapping("queryPrescription")
    public String upload(@RequestParam Long associatorId){
        String value = "?associatorId="+associatorId;
        String url = geturl("/web/auditRecord/queryPrescription") + value;
        return restTemplate.postForObject(url,associatorId,String.class);
    }

    /**
     *会员充值期限列表
     *
     * @return
     */
    @RequestMapping("queryMemberTerm")
    public ResponseEntity<String> upload1(){
        String url = geturl("/web/auditRecord/queryMemberTerm");
        return restTemplate.getForEntity(url,String.class);
    }



    /**
     *会员问诊记录
     *
     * @return
     */
    @RequestMapping("queryInquiry")
    public ResponseEntity<String> upload2(@RequestParam Long associatorId){
        String url = geturl("/web/auditRecord/queryInquiry");
        return restTemplate.postForEntity(url,associatorId,String.class);
    }

    private String geturl(String path){
        ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.ASSOCIATOR_ROUTE);
        //	 String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+path;
        StringBuffer bf = new StringBuffer("http://");
        bf.append(serviceInstance.getHost());
        bf.append(":");
        bf.append(serviceInstance.getPort());
        bf.append(path);
        return bf.toString();

    }
}
